package com.xxzx.publicprevention.temp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.xxzx.publicprevention.mapper.GeoXzqxianDao;
import com.xxzx.publicprevention.pojo.GeoXzqxian;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @作者: thw
 * @Date: 2020/7/29 17:45
 */
@EnableScheduling
@Component
//@EnableAsync  //开启多线程,开启后同一任务也不需要等待。当前业务不需要开启
public class TimedTasks {

    @Resource
    private GeoXzqxianDao geoXzqxianDao;

    //读取json文件
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //    @Scheduled(fixedDelay=5000)//表示上一次任务执行完成后多久再次执行
    public void Test1() {
        String s = readJsonFile("H:\\工作\\柳州国土\\其他项目\\dm\\src\\main\\resources\\static\\data\\XZXZQ柳州市_WGS84.json");
        JSONObject jobj = JSON.parseObject(s);
        JSONArray features = jobj.getJSONArray("features");//构建JSONArray数组
        for (int i = 0; i < features.size(); i++) {
            JSONObject key = (JSONObject) features.get(i);
            JSONObject geometry = (JSONObject) key.get("geometry");
            JSONArray geoms = (JSONArray) geometry.getJSONArray("coordinates");
            //POLYGON((0 0,10,0 10 10, 0 10))
            String PolyStr = "";
            if (geoms.size() > 1) {//多地块情况
                PolyStr = "MULTIPOLYGON(((";
            } else {
                PolyStr = "POLYGON((";
            }
            for (int n = 0; n < geoms.size(); n++) {
                JSONArray coordinates = (JSONArray) geoms.get(n);
                if (coordinates.size() == 1) {//多地块情况
                    JSONArray coordTemp = (JSONArray) coordinates.get(0);
                    for (int j = 0; j < coordTemp.size(); j++) {
                        JSONArray point = (JSONArray) coordTemp.get(j);
                        String lng = ((BigDecimal) point.get(0)).toString();
                        String lat = ((BigDecimal) point.get(1)).toString();
                        PolyStr += lng + " " + lat;
                        if (j < coordTemp.size() - 1) {
                            PolyStr += ",";
                        }

                    }
                    if (n < geoms.size() - 1) {
                        PolyStr += ")),((";
                    }


                } else {
                    for (int j = 0; j < coordinates.size(); j++) {
                        JSONArray point = (JSONArray) coordinates.get(j);
                        String lng = ((BigDecimal) point.get(0)).toString();
                        String lat = ((BigDecimal) point.get(1)).toString();
                        PolyStr += lng + " " + lat;
                        if (j < coordinates.size() - 1){
                            PolyStr += ",";
                        }

                    }

                }

            }

            if (geoms.size() > 1) {//多地块情况
                PolyStr += ")))";
            } else {
                PolyStr += "))";
            }


            JSONObject properties = (JSONObject) key.get("properties");
            String SCSJ = (String) properties.get("SCSJ");
            String XZQMC = (String) properties.get("XZQMC");
            String XZQMC_1 = (String) properties.get("XZQMC_1");
            String XZQDM = (String) properties.get("XZQDM");
            String XZQDM_1 = (String) properties.get("XZQDM_1");

            GeoXzqxian geoXzqxian = new GeoXzqxian();
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
                Date date = simpleDateFormat.parse(SCSJ.split(" ")[0]);
                geoXzqxian.setScsj(date);
            } catch (Exception ee) {

            }
            geoXzqxian.setXianMc(XZQMC_1);
            geoXzqxian.setXzqdm(XZQDM);
            geoXzqxian.setXianDm(XZQDM_1);
            geoXzqxian.setGeom(PolyStr);
            geoXzqxian.setXzqmc(XZQMC);

            int hr = geoXzqxianDao.insert(geoXzqxian);
            if (hr < 0) {
                System.out.println(XZQMC + "——插入失败");
            }
        }
    }
}
